package admin

import (
	"context"

	"go-zero-mall/app/admin/cmd/api/internal/svc"
	"go-zero-mall/app/admin/cmd/api/internal/types"
	"go-zero-mall/app/admin/cmd/rpc/admin"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
	d := admin.AdminLoginReq{
		Username: req.Username,
		Password: req.Password,
	}
	login, err := l.svcCtx.AdminRpc.AdminLogin(l.ctx, &d)
	if err != nil {
		return nil, err
	}
	r := types.LoginResp{
		Token:     login.Token,
		TokenHead: l.svcCtx.Config.JwtAuth.AccessSecret,
	}
	return &r, err
}
